<?php
 require_once 'load.php';
 
 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
 
 switch ($action) {
     case 'login':
            login();
         break;
     case 'logout':
         logout();
         break;
 }

/**
 * @global Twig_Environment $twig 
 */
function login() {
  global  $twig;
  if ( !check_auth() ) {
    $option = array();
    $loginSuccess = true;
    if (isset($_POST['username'])) {

        $username = $_POST['username'];
        $password = $_POST['password'];

        if (do_logon($username, $password)) {

            redirect(BASE_URL .'admin/');
            exit;
        } else {
            $loginSuccess = false;
            $option = array('login' => $loginSuccess);
        }
    }

    echo $twig->render('login.html', $option);
  } else {
      redirect(BASE_URL .'admin/');
  }
}
function logout() {
    do_logout();
}
?>
